#pragma once
#include <iostream>
#include <string>
#include <cstdio>
#include <cstdarg>
#include <ctime>
#include <sys/types.h>
#include <unistd.h>
#include <fstream>
#define DEBUG 1
#define NORMAL 2
#define WARNING 3
#define ERROR 4
#define FATAL 5
const std::string logerr = "./log.error";
const std::string logfile  = "./log.txt";
std::string levelToStr(int level)
{
    switch (level)
    {
    case DEBUG:
        return "DEBUG";
    case NORMAL:
        return "NORMAL";
    case WARNING:
        return "WARNING";
    case ERROR:
        return "ERROR";
    case FATAL:
        return "FATAL";
    default:
        return nullptr;
    }
}
void logMessage(int level, const char *log, ...)
{
#define NUM 1024
    char logprefix[NUM];
    char logcontent[NUM];
    time_t t = time(nullptr);
    struct tm* tm =  localtime(&t);
    
    snprintf(logprefix, sizeof logprefix, "[%s] %dh:%dm:%ds [pid: %d] ", levelToStr(level).c_str(), tm->tm_hour, tm->tm_min, tm->tm_sec, getpid());
    va_list arg;
    va_start(arg, log);
    vsnprintf(logcontent, sizeof logcontent, log, arg);

    // std::ofstream ofslog(logfile, std::ofstream::app);
    // std::ofstream ofserr(logerr, std::ofstream::app);

    // if(level == DEBUG || level == NORMAL)
    // {
    //     ofslog << logprefix << logcontent << std::endl;
    // }
    // else if(level == WARNING || level == ERROR || level == FATAL)
    // {
    //     ofserr << logprefix << logcontent << std::endl;
    // }

    // ofslog.close();
    // ofserr.close();

    std::cout << logprefix << logcontent << std::endl;

}